package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 叙词模版Controller
 * 
 * @author ruoyi
 * @date 2022-08-22
 */
@RestController
@RequestMapping("/system/thesauriWordModule")
public class SysThesauriWordModuleController extends BaseController
{
    @Autowired
    private ISysThesauriWordModuleService sysThesauriWordModuleService;

    @Autowired
    private ISysThesauriWordRelationService sysThesauriWordRelationService;

    @Autowired
    private ISysThesauriWordCategoryService sysThesauriWordCategoryService;

    @Autowired
    private ISysThesauriWordReferenceService sysThesauriWordReferenceService;

    @Autowired
    ISysThesauriWordService sysThesauriWordService;

    /**
     * 查询叙词模版列表
     */
    @PreAuthorize("@ss.hasAnyPermi('system:thesauriWordModule:list,system:thesauriWordRelation:list,system:thesauriWordCategory:list,system:thesauriWord:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysThesauriWordModule sysThesauriWordModule)
    {
        startPage();
        List<SysThesauriWordModule> list = sysThesauriWordModuleService.selectSysThesauriWordModuleList(sysThesauriWordModule);
        SysThesauriWordRelation thesauriWordRelation = null;
        for (SysThesauriWordModule thesauriWordModule : list) {
            thesauriWordRelation = new SysThesauriWordRelation();
            thesauriWordRelation.setThesauriWordModuleId(thesauriWordModule.getId());
            List<SysThesauriWordRelation> sysThesauriWordRelations = sysThesauriWordRelationService.selectSysThesauriWordRelationList(thesauriWordRelation);
            if(sysThesauriWordRelations.size() == 0){
                thesauriWordRelation = new SysThesauriWordRelation();
                thesauriWordRelation.setNameZh("用");
                thesauriWordRelation.setNameEn("Y");
                thesauriWordRelation.setSort(1L);
                thesauriWordRelation.setThesauriWordModuleId(thesauriWordModule.getId());
                sysThesauriWordRelationService.insertSysThesauriWordRelation(thesauriWordRelation);
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出叙词模版列表
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordModule:export')")
    @Log(title = "叙词模版", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysThesauriWordModule sysThesauriWordModule)
    {
        List<SysThesauriWordModule> list = sysThesauriWordModuleService.selectSysThesauriWordModuleList(sysThesauriWordModule);
        ExcelUtil<SysThesauriWordModule> util = new ExcelUtil<SysThesauriWordModule>(SysThesauriWordModule.class);
        return util.exportExcel(list, "叙词模版数据");
    }

    /**
     * 获取叙词模版详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordModule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysThesauriWordModuleService.selectSysThesauriWordModuleById(id));
    }

    /**
     * 新增叙词模版
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordModule:add')")
    @Log(title = "叙词模版", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysThesauriWordModule sysThesauriWordModule)
    {
        return toAjax(sysThesauriWordModuleService.insertSysThesauriWordModule(sysThesauriWordModule));
    }

    /**
     * 修改叙词模版
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordModule:edit')")
    @Log(title = "叙词模版", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysThesauriWordModule sysThesauriWordModule)
    {
        return toAjax(sysThesauriWordModuleService.updateSysThesauriWordModule(sysThesauriWordModule));
    }

    /**
     * 删除叙词模版
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordModule:remove')")
    @Log(title = "叙词模版", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        for (Long id : ids) {
            removeRelation(id);
            removeCategory(id);
            removeWord(id);
            removeReference(id);
            sysThesauriWordModuleService.deleteSysThesauriWordModuleById(id);
        }
        return toAjax(1);
    }

    //删除叙词关系
    private void removeRelation(Long id) {
        SysThesauriWordRelation thesauriWordRelation = new SysThesauriWordRelation();
        thesauriWordRelation.setThesauriWordModuleId(id);
        List<SysThesauriWordRelation> sysThesauriWordRelations = sysThesauriWordRelationService.selectSysThesauriWordRelationList(thesauriWordRelation);
        for (SysThesauriWordRelation sysThesauriWordRelation : sysThesauriWordRelations) {
            sysThesauriWordRelationService.deleteSysThesauriWordRelationById(sysThesauriWordRelation.getId());
        }
    }

    //删除叙词分类
    private void removeCategory(Long id) {
        SysThesauriWordCategory thesauriWordCategory = new SysThesauriWordCategory();
        thesauriWordCategory.setThesauriWordModuleId(id);
        List<SysThesauriWordCategory> sysThesauriWordCategories = sysThesauriWordCategoryService.selectSysThesauriWordCategoryList(thesauriWordCategory);
        for (SysThesauriWordCategory sysThesauriWordCategory : sysThesauriWordCategories) {
            sysThesauriWordCategoryService.deleteSysThesauriWordCategoryById(sysThesauriWordCategory.getId());
        }
    }

    //删除叙词
    private void removeWord(Long id) {
        SysThesauriWord thesauriWord = new SysThesauriWord();
        thesauriWord.setThesauriWordModuleId(id);
        List<SysThesauriWord> sysThesauriWords = sysThesauriWordService.selectSysThesauriWordList(thesauriWord);
        for (SysThesauriWord sysThesauriWord : sysThesauriWords) {
            sysThesauriWordService.deleteSysThesauriWordById(sysThesauriWord.getId());
        }
    }

    //删除叙词引用
    private void removeReference(Long id) {
        SysThesauriWordReference thesauriWordReference = new SysThesauriWordReference();
        thesauriWordReference.setThesauriWordModuleId(id);
        List<SysThesauriWordReference> sysThesauriWordReferences = sysThesauriWordReferenceService.selectSysThesauriWordReferenceList(thesauriWordReference);
        for (SysThesauriWordReference sysThesauriWordReference : sysThesauriWordReferences) {
            sysThesauriWordReferenceService.deleteSysThesauriWordReferenceById(sysThesauriWordReference.getId());
        }
    }
}
